package com.banciyuan.bcywebview.biz.detail.goods;

import android.content.Context;

import com.android.volley.Request;
import com.android.volley.RequestQueue;
import com.android.volley.Response;
import com.android.volley.VolleyError;
import com.banciyuan.bcywebview.api.ZhipinApi;
import com.banciyuan.bcywebview.base.helper.data.UserDataHelper;
import com.banciyuan.bcywebview.utils.http.BcyPair;
import com.banciyuan.bcywebview.utils.http.HttpUtils;
import com.banciyuan.bcywebview.utils.http.OnErrorProxy;
import com.banciyuan.bcywebview.utils.http.StatusCodeUtils;
import com.banciyuan.bcywebview.utils.http.StringRequestParam;
import com.banciyuan.bcywebview.utils.http.VolleyQueue;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;

/**
 * Created by squall on 2016/10/31.
 */

public class GoodsHelper {

    private Context mContext;
    private RequestQueue mQueue;
    private boolean posting;

    public GoodsHelper(Context mContext) {
        this.mContext = mContext;
        this.mQueue = VolleyQueue.getRquest(mContext);
    }

    public void getGoodsDetail(String zg_id, final GoodsHelperInter goodsHelperInter) {
        String url = HttpUtils.BASE_URL + ZhipinApi.getDetail();
        List<BcyPair> pairList = new ArrayList<>();
        pairList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(mContext).getToken()));
        pairList.add(new BcyPair(HttpUtils.ZGID, zg_id));
        Map<String, String> map = HttpUtils.getData(pairList);
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                    goodsHelperInter.onSucc(response);
                } else {
                    goodsHelperInter.onFail(response);
                }
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                goodsHelperInter.onFail("");
            }
        };
        OnErrorProxy onErrorProxy = new OnErrorProxy(onError, onSucc, url, mContext, map);
        StringRequestParam requestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onErrorProxy);
        mQueue.add(requestParam);
    }

    public void postDing(String zg_id, final GoodsHelperInter goodsHelperInter, boolean ding) {
        if (posting) return;
        posting = true;
        String url = HttpUtils.BASE_URL;
        if (ding) {
            url += ZhipinApi.getDoPostDing();
        } else {
            url += ZhipinApi.getUnDoPostDing();
        }
        List<BcyPair> pairList = new ArrayList<>();
        pairList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(mContext).getToken()));
        pairList.add(new BcyPair(HttpUtils.ZGID, zg_id));
        Map<String, String> map = HttpUtils.getData(pairList);
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                    goodsHelperInter.onSucc(response);
                } else {
                    goodsHelperInter.onFail(response);
                }
                posting = false;
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                goodsHelperInter.onFail("");
                posting = false;
            }
        };
        OnErrorProxy onErrorProxy = new OnErrorProxy(onError, onSucc, url, mContext, map);
        StringRequestParam requestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onErrorProxy);
        mQueue.add(requestParam);
    }

    public void postTuijian(String zg_id, final GoodsHelperInter goodsHelperInter, boolean tuijian) {
        if (posting) return;
        posting = true;
        String url = HttpUtils.BASE_URL;
        if (tuijian) {
            url += ZhipinApi.getDoTuijian();
        } else {
            url += ZhipinApi.getUnDoTuijian();
        }
        List<BcyPair> pairList = new ArrayList<>();
        pairList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(mContext).getToken()));
        pairList.add(new BcyPair(HttpUtils.ZGID, zg_id));
        Map<String, String> map = HttpUtils.getData(pairList);
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                    goodsHelperInter.onSucc(response);
                } else {
                    goodsHelperInter.onFail(response);
                }
                posting = false;
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                goodsHelperInter.onFail("");
                posting = false;
            }
        };
        OnErrorProxy onErrorProxy = new OnErrorProxy(onError, onSucc, url, mContext, map);
        StringRequestParam requestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onErrorProxy);
        mQueue.add(requestParam);
    }

    public void postDenounce(String zg_id, final GoodsHelperInter goodsHelperInter) {
        if (posting) return;
        posting = true;
        String url = HttpUtils.BASE_URL + ZhipinApi.getDenouncePost();
        List<BcyPair> pairList = new ArrayList<>();
        pairList.add(new BcyPair(HttpUtils.TOKEN, UserDataHelper.getInstance(mContext).getToken()));
        pairList.add(new BcyPair(HttpUtils.ZGID, zg_id));
        Map<String, String> map = HttpUtils.getData(pairList);
        Response.Listener<String> onSucc = new Response.Listener<String>() {
            @Override
            public void onResponse(String response) {
                if (StatusCodeUtils.dealStatusCode(response, mContext)) {
                    goodsHelperInter.onSucc(response);
                } else {
                    goodsHelperInter.onFail(response);
                }
                posting = false;
            }
        };

        Response.ErrorListener onError = new Response.ErrorListener() {
            @Override
            public void onErrorResponse(VolleyError error) {
                goodsHelperInter.onFail("");
                posting = false;
            }
        };
        OnErrorProxy onErrorProxy = new OnErrorProxy(onError, onSucc, url, mContext, map);
        StringRequestParam requestParam = new StringRequestParam(Request.Method.POST, url, map, onSucc, onErrorProxy);
        mQueue.add(requestParam);
    }
}
